package com.daka.service.impl;

import com.daka.dao.OrderDao;
import com.daka.entity.Order;
import com.daka.entity.Project;
import com.daka.entity.Team;
import com.daka.entity.User;
import com.daka.enumerate.StatusCode;
import com.daka.exception.ServiceRuntimeException;
import com.daka.service.OrderService;
import com.daka.service.ProjectService;
import com.daka.service.TeamService;
import com.daka.service.UserService;
import com.daka.vo.db.OrderInfoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author beliveli on 2016/3/31 0031.
 */
@Service("orderService")
@SuppressWarnings("unused")
public class OrderServiceImpl implements OrderService {

    private static Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Resource
    private OrderDao orderDao;
    @Autowired
    private TeamService teamService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProjectService projectService;


    @Override
    public Integer findAllCount() {
        Order order = new Order();
        return orderDao.selectByStatementCount(order);
    }

    @Override
    public int insert(Order order) {
        logger.info("保存订单信息： 团队teamId:{}; 项目projectId:{}; 用户userId:{}; 总额（元）money:{}", order.getTeamId(), order.getProjectId(), order.getUserId(), order.getMoney());
        BigDecimal a = new BigDecimal("0");
        if (order.getMoney() == null || order.getMoney().compareTo(a) <= 0) {
            throw new ServiceRuntimeException(StatusCode.COMMON_PARAM_ERROR.getCode(), StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        if (order.getTeamId() != null) {
            Team team = teamService.findById(order.getTeamId());
            if (team != null) {
                team.setTotalMoney(team.getTotalMoney().add(order.getMoney()));
                teamService.updateById(team);
            }
        }

        if (order.getProjectId() != null) {
            Project project = projectService.findById(order.getProjectId());
            if (project != null) {
                project.setMoney(project.getMoney().add(order.getMoney()));
                projectService.updateById(project);
            }
        }
        if (order.getUserId() != null) {
            User user = userService.selectById(order.getUserId());
            if (user != null) {
                user.setMoney(user.getMoney().add(order.getMoney()));
                userService.updateById(user);
            }
        }
        order.setCreateTime(new Date());
        int b = orderDao.insert(order);
        return b;
    }

    /**
     * userId、projectId、money
     */
    @Override
    public int create(Order order) {
        logger.info("保存订单信息 项目projectId:{}; 用户userId:{}; 总额（元）money:{}", order.getProjectId(), order.getUserId(), order.getMoney());
        //修改捐赠项目总金额
        if (order.getProjectId() != null) {
            Project project = projectService.findById(order.getProjectId());
            if (project != null) {
                project.setMoney(project.getMoney().add(order.getMoney()));
                projectService.updateById(project);
            }
            if (project != null && project.getTeamId() != null) {
                Team team = teamService.findById(project.getTeamId().longValue());
                if (team != null) {
                    team.setTotalMoney(team.getTotalMoney().add(order.getMoney()));
                    teamService.updateById(team);
                }
                order.setTeamId(project.getTeamId().longValue());
            }
        }
        //修改用户捐赠总金额
        if (order.getUserId() != null) {
            User user = userService.selectById(order.getUserId());
            if (user != null) {
                user.setMoney(user.getMoney().add(order.getMoney()));
                userService.updateById(user);
            }
        }
        order.setCreateTime(new Date());
        int b = orderDao.insert(order);
        return b;
    }

    @Override
    public int createOnly(Order order) {
        logger.info("保存订单信息Only 项目projectId:{}; 用户userId:{}; 总额（元）money:{}", order.getProjectId(), order.getUserId(), order.getMoney());
        order.setCreateTime(new Date());
        int b = orderDao.insert(order);
        return b;
    }

    public int count(Order order) {
        return orderDao.selectByStatementCount(order);
    }

    public List<Order> selectOrderList(Order order) {
        return orderDao.selectList(order, " createTime desc");
    }

    @Override
    public List<Order> selectByStatement(Order order) {
        return orderDao.selectByStatement(order);
    }

    @Override
    public List<Order> selectByTeamId(Long orderId) {
        return orderDao.selectByTeamId(orderId);
    }

    @Override
    public Order selectByTransactionId(String transactionId) {
        return orderDao.selectByTransactionId(transactionId);
    }

    @Override
    public List<OrderInfoVo> findAll() {
        return orderDao.findAll();
    }
}



